<?php
/**
 * @author 天涯 <mail@phpha.com>
 * @copyright http://blog.phpha.com
 * @link http://blog.phpha.com/vncms
 * @version 1.0
 */
defined('APP_PATH') or exit('Access Denied');
require 'check.php';
class menu extends check{
	//构造函数
	public function __construct(){
		parent::__construct();
		global $__action;
		$arr = array();
		$menu = $__action == 'upd' ? spClass('m_menu')->findAll("id != ".intval($this->spArgs('id')),'odnum ASC, id ASC') : spClass('m_menu')->findAll(null,'odnum ASC, id ASC');
		foreach($menu as $v){
			$arr[] = array('id'=>$v['id'],'pid'=>$v['pid'],'name'=>$v['name'],'url'=>$v['url'],'odnum'=>$v['odnum']);
		}
		$treeObj = spClass('Tree',array(array('id','pid','name','format')));
		$this->menu = $treeObj->getTree($arr);
	}
	//菜单列表
	public function lis(){
		$this->display('menu_lis.tpl.php');
	}
	//添加菜单
	public function add(){
		$this->display('menu_add.tpl.php');
	}
	public function doadd(){
		if(false != spClass('m_menu')->spVerifier($this->spArgs()))	$this->error('数据验证错误！',spUrl('menu','add'));
		if(false != spClass('m_menu')->create($this->spArgs())){
			$this->success('添加菜单成功！',spUrl('menu','lis'));
		}else{
			$this->error('添加菜单失败！',spUrl('menu','add'));
		}
	}
	//更新菜单
	public function upd(){
		if(!$id = intval($this->spArgs('id')))	$this->error('缺少必要参数！',spUrl('menu','lis'));
		$this->info = spClass('m_menu')->find(array('id'=>$id));
		$this->display('menu_upd.tpl.php');
	}
	public function doupd(){
		if(false != spClass('m_menu')->spVerifier($this->spArgs()))	$this->error('数据验证错误！',spUrl('menu','lis'));
		$menu = $this->spArgs();
		if($menu['pid'] == 'default')	unset($menu['pid']);
		if(false != spClass('m_menu')->update(array('id'=>$menu['id']),$menu)){
			$this->success('更新菜单成功！',spUrl('menu','lis'));
		}else{
			$this->error('更新菜单失败！',spUrl('menu','lis'));
		}
	}
	//删除菜单
	public function del(){
		if(!$id = intval($this->spArgs('id')))	$this->error('缺少必要参数！',spUrl('menu','lis'));
		if(false != spClass('m_menu')->find(array('pid'=>$id))){
			$this->error('存在子菜单禁止删除！',spUrl('menu','lis'));
		}elseif(false != spClass('m_menu')->delete(array('id'=>$id))){
			$this->success('删除菜单成功！',spUrl('menu','lis'));
		}else{
			$this->error('删除菜单失败！',spUrl('menu','lis'));
		}
	}
	//更新排序
	public function updod(){
		$idrow = $this->spArgs('idrow');
		$odnum = $this->spArgs('odnum');
		for($i=0;$i<count($idrow);$i++){
			spClass('m_menu')->update(array('id'=>$idrow[$i]),array('odnum'=>(int)$odnum[$i]));
		}
		$this->success('更新排序成功！',spUrl('menu','lis'));
	}
	//配置菜单
	public function conf(){
		if(!$id = intval($this->spArgs('id')))	$this->error('缺少必要参数！',spUrl('group','lis'));
		//当前用户组		
		if(!$this->group = spClass('m_group')->find(array('id'=>$id))) $this->error('管理组不存在！',spUrl('group','lis'));
		//全部用户组
		$this->groups = spClass('m_group')->findAll();
		//全部菜单
		$menus = $myMenus = array();
		foreach($this->menu as $v){
			if($v['pid'] == 0){
				if(isset($menus[$v['pid']]['child'])){
					$v['child'] = $menus[$v['pid']]['child'];
				}
				$menus[$v['id']] = $v;
			}else{
				$menus[$v['pid']]['child'][] = $v;
			}
		}
		$this->menus = $menus;
		//已分配菜单
		$myMenu = spClass('m_gmenu')->findAll(array('group_id'=>$id));
		foreach($myMenu as $v){
			$myMenus[] = $v['menu_id'];
		}
		$this->myMenus = $myMenus;
		$this->display('menu_conf.tpl.php');
	}
	public function doconf(){
		$group_id = $this->spArgs('group_id');
		$menus = $this->spArgs('menu');
		if(empty($group_id)) $this->error('请选择管理组！',spUrl('group','lis'));
		if(empty($menus)) $this->error('菜单不能为空！',spUrl('group','lis'));
		//循环执行删除插入
		foreach($group_id as $id){
			spClass('m_gmenu')->delete(array('group_id'=>$id));
			foreach($menus as $menu){
				spClass('m_gmenu')->create(array('group_id'=>$id, 'menu_id'=>$menu));
			}
		}
		//清除缓存文件
		foreach($group_id as $id){
			$filename = APP_PATH.'/cache/group_'.$id.'.menu';
			if(file_exists($filename)){
				unlink($filename);
			}
		}
		$this->success('配置管理组菜单成功！', spUrl('group','lis'));
	}
}